package com.linzm.leetcode.mid.tree.其他;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author zimingl
 * @Date 2023/2/25 0:08
 * @Description: TODO
 */
public class Demo9MaxDepth559 {
    public int maxDepth(Node root) {
        if (root == null) return 0;
        Deque<Node> queue = new LinkedList<>();
        queue.offer(root);
        int depth = 0;
        while (!queue.isEmpty()) {
            int len = queue.size();
            while (len-- > 0) {
                Node node = queue.poll();
                for (Node tmp : node.children) {
                    if (tmp != null) {
                        queue.offer(tmp);
                    }
                }
            }
            depth++;
        }
        return depth;
    }

    /*递归法，后序遍历求root节点的高度*/
    public int maxDepth2(Node root) {
        if (root == null) return 0;
        int depth = 0;
        if (root.children != null) {
            for (Node child : root.children) {
                depth = Math.max(depth, maxDepth2(child));
            }
        }
        return depth + 1; //中节点
    }
}
